from database.models import Videos, Video_details
from utils import get_more_details
from user.login import check_auth
from django.http import HttpRequest, JsonResponse, HttpResponse


def getdetail(request: HttpRequest):
    token = request.GET.get('token')
    vid_id = request.GET.get('id')

    if not vid_id:
        return HttpResponse('Invalid request', status=400)

    user_id = -1
    if token and type(check_auth(token)) == int:
        user_id = check_auth(token)

    return JsonResponse(get_more_details(Videos.objects.filter(video_id=vid_id).first(), user_id))
    # todo: vid_info


def operations(request: HttpRequest):
    token = request.GET.get('token')
    vid_id = request.GET.get('id')
    op = request.GET.get('operation')

    if not vid_id or not op:
        return HttpResponse('Invalid request, no id or operation', status=400)
    id = check_auth(token)
    if type(id) != int:
        return HttpResponse(str(id), status=400)

    if Video_details.objects.filter(user_id=id, video_id=vid_id, operation_type=op).first() is None:
        Video_details.objects.create(user_id=id, video_id=vid_id, operation_type=op)
        return HttpResponse('success')
    return HttpResponse('Invalid operation, operation was happened', status=400)


def disoperations(request: HttpRequest):
    token = request.GET.get('token')
    vid_id = request.GET.get('id')
    op = request.GET.get('operation')

    if not vid_id or not op:
        return HttpResponse('Invalid request, no id or operation', status=400)
    id = check_auth(token)
    if type(id) != int:
        return HttpResponse(str(id), status=400)

    vid_obj = Video_details.objects.filter(user_id=id, video_id=vid_id, operation_type=op).first()

    if vid_obj is not None:
        Video_details.delete(vid_obj)
        return HttpResponse('success')
    return HttpResponse('Invalid operation, operation was happened', status=400)
